package com.coc.service;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
//@RabbitListener(queues = "queue_test1")
public class RabbitConsumer {

//    @Autowired
//    private AmqpTemplate amqpTemplate;


//    @RabbitHandler
//    public void recievedMq(String message){
//        System.out.println( message == null ? "null": "消费者接收： " + message);
//    }

//    @RabbitHandler
    @RabbitListener(queues = "queue_test1")
    public void recievedMq  (Message message, Channel channel) throws IOException {

        // 采用手动应答模式, 手动确认应答更为安全稳定
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);

        System.out.println( message == null ? "null": "消费者接收： " + new String(message.getBody()) +"["+ message+"] - - " + message.getMessageProperties().getDeliveryTag() );
    }


    @RabbitListener(queues = "queue_test1")
    public void recievedMq2  (Message message, Channel channel) throws IOException {

        // 采用手动应答模式, 手动确认应答更为安全稳定
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);

        System.out.println( message == null ? "null": "消费者接收2： " + new String(message.getBody()) +"["+ message+"] - - " + message.getMessageProperties().getDeliveryTag() );
    }

}
